package com.tuzhi.sys.web.controller;

import com.tuzhi.base.service.BaseService;
import com.tuzhi.base.web.controller.SimpleBaseController;
import com.tuzhi.base.web.helper.AjaxResultHelper;
import com.tuzhi.base.web.vo.AjaxResultVO;
import com.tuzhi.common.constants.ResultConstant;
import com.tuzhi.common.domain.ResultBO;
import com.tuzhi.common.helper.ResultHelper;
import com.tuzhi.sys.dto.SysMenuTreeDto;
import com.tuzhi.sys.jpa.entity.SysMenuEntity;
import com.tuzhi.sys.scurity.AuthService;
import com.tuzhi.sys.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/menu")
@Api(tags = "菜单")
@Slf4j
public class SysMenuController extends SimpleBaseController<SysMenuEntity, Long, Object> {
    @Autowired
    SysMenuService sysMenuService;
    @Autowired
    AuthService authService;

    @Override
    protected BaseService getThisService() {
        return sysMenuService;
    }

    @ApiOperation("保存")
    @PutMapping({"/save"})
    @Override
    public ResultBO save(@RequestBody SysMenuEntity entity) {
        if (entity == null) {
            return ResultHelper.build(ResultConstant.ERR_PARAM_NULL);
        } else {
            log.info(entity.toString());
            authService.saveMenu(entity);
            return ResultHelper.success();
        }
    }

    @ApiOperation("查询菜单树")
    @GetMapping({"/menuTree"})
    public AjaxResultVO<List<SysMenuTreeDto>> getMenuTree() {
        List<SysMenuTreeDto> menuTreeDtos = sysMenuService.loadAllMenuTree();
        return AjaxResultHelper.success().ofData(menuTreeDtos);
    }

    @ApiOperation("删除")
    @DeleteMapping({"del/{id}"})
    public ResultBO del(@PathVariable(value = "id", required = true) Long id) {
        authService.deleteMenu(id);
        return ResultHelper.success();
    }

}
